module neo.tetris.view {
	/**
	 *
	 * @author 
	 *
	 */
	export class MatchView extends eui.Component {
        public player0: neo.tetris.view.MatchViewPlayer;
        public matchLabel: eui.Label;
        public player1: neo.tetris.view.MatchViewPlayer;
        
		public constructor() {
            super();
            
            this.addEventListener(egret.Event.ADDED_TO_STAGE,this.handleEvent,this);
            this.addEventListener(egret.Event.REMOVED_FROM_STAGE,this.handleEvent,this);
            this.skinName = "resource/custom_skins/MatchViewSkin.exml";
		}

        private key: number;
		private handleEvent(evt:egret.Event):void {
		    switch (evt.type) {
                case egret.Event.ADDED_TO_STAGE:
                    neo.tetris.model.appInstance.battle.addEventListener(neo.typescript.model.Bean.PROPERTY_CHANGE,this.handleModelChanged,this);
                    this.handleModelChanged();
                    // matchLabel
                    this.key = egret.setInterval(this.refreshMatchTxt,this,500);
                    this.player0.x = this.player1.x = (this.width - this.player0.width) >> 1;
                    break;
                case egret.Event.REMOVED_FROM_STAGE:
                    neo.tetris.model.appInstance.battle.removeEventListener(neo.typescript.model.Bean.PROPERTY_CHANGE,this.handleModelChanged,this);
                    egret.clearInterval(this.key);
                    break;
		    }
		}
		
        private handleModelChanged(evt:egret.Event = null):void {
            if(!evt || evt.data == neo.tetris.model.BattleModel.PLAYERS) {
                // player0
                if(neo.tetris.model.appInstance.battle.players && neo.tetris.model.appInstance.battle.players[0]) {
                    this.player0.nameTxt.text = neo.tetris.model.appInstance.battle.players[0].name;
                    this.player0.alpha = .0;
                    this.player0.headIcon.source = neo.tetris.model.appInstance.battle.players[0].headIcon;
                    egret.Tween.get(this.player0).to({ x: -50 + ((this.width - this.player0.width) >> 1), alpha: 1.0 },300,egret.Ease.sineIn);
                    egret.clearInterval(this.key);
                } else {
                    this.player0.alpha = .0;
                }
                // player1
                if(neo.tetris.model.appInstance.battle.players && neo.tetris.model.appInstance.battle.players[1]) {
                    this.player1.nameTxt.text = neo.tetris.model.appInstance.battle.players[1].name;
                    this.player1.alpha = .0;
                    this.player1.headIcon.source = neo.tetris.model.appInstance.battle.players[1].headIcon;
                    egret.Tween.get(this.player1).to({ x: 50 + ((this.width - this.player0.width) >> 1),  alpha: 1.0 },300,egret.Ease.sineIn);
                    egret.clearInterval(this.key);
                } else {
                    this.player1.alpha = .0;
                }
            }
     	}
		
		private refreshMatchTxt():void {
    		if (this.matchLabel.text == "matching...") {
                this.matchLabel.text = "matching.";
    		} else {
                this.matchLabel.text += ".";    		    
    		}
		}
		
        public fadeOut(duration:number):void {
            egret.Tween.get(this.player0).wait(300).to({ x: -100 + ((this.width - this.player0.width) >> 1),alpha: .0 },300,egret.Ease.sineIn);
            egret.Tween.get(this.player1).wait(300).to({ x: 100 + ((this.width - this.player1.width) >> 1),alpha: .0 },300,egret.Ease.sineIn);
            var t: MatchView = this;
            egret.setTimeout(
                function():void {
                    t.parent.removeChild(t);
                }, this, 700
            );
        }
	}
}
